source(here::here("code/load.R"))

dat = readRDS(here("data/data_2021-08.rds"))
setDT(dat)

#####################################################################
#  approval for fiscal redistribution and support for coal buyouts  #
#####################################################################
cm <- c("Democrat" = "Democrat",
        "genderMale" = "Man",
        "college" = "College",
        "age" = "Age",
        "redistribution" = "Redistribution approval")
tmp <- data.table(dat)[
    is.na(covid_check), covid_check := "Not asked"][
    , Democrat := fifelse(party == "Democrat", 1, 0)]
mod <- lm(coal_compensate ~ redistribution + age + gender + college + Democrat, data = tmp)
p <- modelplot(mod, coef_map = cm, vcov = "robust") +
     geom_vline(xintercept = 0, linetype = 3) +
     theme_vab()
ggsave(p, file = here("txt/fig/dot_redistribution_2021-08.pdf"), width = 5, height = 2)

tmp <- data.table(dat)[
    is.na(covid_check), covid_check := "Not primed"][
    , covid_check := factor(covid_check, levels = c("Not primed", "No", "Yes"))][
    , Democrat := fifelse(party == "Democrat", 1, 0)]
cm <- c("Democrat" = "Democrat",
        "genderMale" = "Man",
        "college" = "College",
        "age" = "Age",
        "covid_checkYes" = "Covid-19 relief: Yes",
        "covid_checkNo" = "Covid-19 relief: No")

mod <- lm(coal_compensate ~ covid_check + age + gender + college + Democrat, data = tmp)
p <- modelplot(mod, coef_map = cm, vcov = "robust") +
     geom_vline(xintercept = 0, linetype = 3) +
     theme_vab()
ggsave(p, file = here("txt/fig/coal_covid_check_2021-08.pdf"), width = 5, height = 2)



############################
## Descriptive statistics ##
############################

variable_labels = c(
    'Buyout plan' = 'coal_treatment',
    'Covid check' = 'covid_check',
    'Education' = 'education_census',
    'Gender' = 'gender',
    'Age' = 'age_census',
    'Income' = 'income',
    'Party ID' = 'party',
    'Support: ``buyout"' = 'coal_compensate',
    'Support: ``do everything"' = 'coal_end')

tmp = copy(dat)[
    is.na(gender), gender := "Other"][
  , ..variable_labels]

setnames(tmp, old = variable_labels, new = names(variable_labels))

datasummary_skim(
    tmp,
    output = here("txt/fig/skim_numeric_2021-08.tex"),
    title = "Descriptive statistics for continuous variables collected in the August 2021 survey on coal buyouts.")

datasummary_skim(
    tmp,
    type = "categorical",
    title = "Descriptive statistics for categorical variables collected in the August 2021 survey on coal buyouts.",
    output = here("txt/fig/skim_categorical_2021-08.tex"))


#############################################################
## Does introducing the idea of a buyout change attitudes? ##
## No.                                                     ##
## Only Do Everything it can                               ##
#############################################################

tmp = copy(dat)
tmp = tmp[, .(respondent, coal_treatment, coal_compensate, coal_end)]
tmp[, coal_treatment := fifelse(coal_treatment == "Gradual",
                                "Late exposure to buyout",
                                "Early exposure to buyout")]

p = ggplot(tmp, aes(x = coal_end, fill = coal_treatment)) +
    geom_histogram(position = "dodge") +
    scale_fill_grey() +
    labs(x = "Support for government action",
         y = "Number of survey responses") +
    theme_vab() +
    theme(legend.position = "top",
          legend.title = element_blank())
ggsave(p, file = here("txt/fig/coal_gradual_vs_immediate_rhs_only_2021-08.pdf"), width = 5, height = 4)
